package com.example.kingdee.kingdeeBridge.kingdeeImpl.impl;

import com.example.kingdee.common.StringUtils;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.BillWriteInDto;
import com.example.kingdee.kingdeeBridge.entity.SrcBillEntry;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.BillImpl;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.common.DataObject;
import com.example.kingdee.sqlTool.common.QueryObject;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

/**
 * 销售出库接口实现类
 */
@Service
public class SalOutStockImpl implements BillImpl {

    @Autowired
    private SqlTool sqlTool;

    @Autowired
    private TBxStMapper tBxStMapper;

    @Override
    public Map<String, String> selectTargetBillEntry(String fid) {
        //查询销售出库单
        QueryObject inventorySelect = new QueryObject(FormIdEnum.salOutStock);
        ArrayList<String> resultKeyArray = new ArrayList<>();
        inventorySelect.addSelect("FEntity_FENTRYID");//明细行id
        resultKeyArray.add("entryId");
        inventorySelect.addSelect("FEntity_Link_FSId");//来源单据分录内码
        resultKeyArray.add("srcEntryId");
        inventorySelect.addCondition("fid", fid);
        ArrayList<HashMap<String, Object>> targetEntryInfo = sqlTool.weaveResult(inventorySelect, resultKeyArray);
        //构建收料通知单明细行id和原单行id对应关系
        Map<String, String> keyValue = new HashMap<>();
        for (HashMap<String, Object> sld : targetEntryInfo) {
            keyValue.put(sld.get("srcEntryId").toString(), sld.get("entryId").toString());
        }
        return keyValue;
    }

    @Override
    public RequestResult updateTargetBillEntry(String fid, BillWriteInDto billWriteInDto, Map<String, String> targetInfo) {
        List<SrcBillEntry> srcBillEntryList = billWriteInDto.getSrcBillEntryList();
        //将源单明细按照行id进行分类
        MultiValueMap<String, SrcBillEntry> classFySrcBillEntry = sqlTool.getClassFy(srcBillEntryList);
        //修改收料数量，仓库
        List<Map<String, Object>> detailEntityList = new ArrayList<>();
        for (String srcEntryId : classFySrcBillEntry.keySet()) {
            List<SrcBillEntry> srcBillEntries = classFySrcBillEntry.get(srcEntryId);
            for (int i = 0; i < srcBillEntries.size(); i++) {
                SrcBillEntry srcBillEntry = srcBillEntries.get(i);
                //构建出库单和配货单关联关系
                Map<String,Object> detailEntity = new LinkedHashMap<>();
                if (i == 0){
                    //修改下推的行
                    detailEntity.put("FEntryID", targetInfo.get(srcEntryId));//行id 必填
                }else {
//                    Map<String, Object> srcPoorInfo = new HashMap<>();
//                    if (billWriteInDto.getBillType().equals("生产入库")){
//                        //查询生产订单原单信息
//                        srcPoorInfo = tBxStMapper.getSrcPrdMoInfo(srcEntryId);
//                    }else {
//                        //查询汇报单明细
//                        srcPoorInfo = tBxStMapper.getSrcPpBomInfo(srcEntryId);
//                    }
//                    //构建基础信息
//                    detailEntity.put("FEntryID", 0);//行id 必填
//                    detailEntity.put("FSrcEntryId", srcEntryId);//源单分录内码 必填
//                    HashMap<String, Object> ownerId = new HashMap<>();
//                    ownerId.put("fnumber", "101");
//                    HashMap<String, Object> stockStatusId = new HashMap<>();
//                    stockStatusId.put("fnumber", "10000");
//                    HashMap<String, Object> stockUnitId = new HashMap<>();
//                    stockUnitId.put("fnumber", srcPoorInfo.get("unitNumber"));
//                    HashMap<String, Object> materialId = new HashMap<>();
//                    materialId.put("fnumber", srcPoorInfo.get("itemNumber"));
//                    detailEntity.put("FMaterialId", materialId);//物料id 必填
//                    detailEntity.put("FMustQty", srcBillEntry.getQty());//应收数量
//                    detailEntity.put("FInStockType", "1");//入库类型
//                    detailEntity.put("FUnitID", stockUnitId);//单位 必填
//                    detailEntity.put("FBaseUnitId", stockUnitId);//基本单位 必填
//                    detailEntity.put("FOwnerTypeId", srcPoorInfo.get("ownerType"));//货主类型 必填
//                    detailEntity.put("FOwnerId", ownerId);//货主 必填
//                    detailEntity.put("FMoBillNo", srcPoorInfo.get("srcBillNo"));//生产订单编号 必填
//                    detailEntity.put("FKeeperTypeId", srcPoorInfo.get("ownerType"));//保管者类型 必填
//                    detailEntity.put("FKeeperId", ownerId);//保管者类型 必填
//                    detailEntity.put("FStockStatusId", stockStatusId);//库存状态 必填
//
//                    detailEntity.put("FSrcBillNo", srcPoorInfo.get("srcBillNo"));//源单编号 必填
//                    detailEntity.put("FSrcEntryId", Integer.parseInt(srcEntryId));//源单分录内码 必填
//
//                    //构建原单关联关系
//                    List<Map<String, Object>> detailLinkList = new ArrayList<>();
//                    HashMap<String, Object> detailLink = new HashMap<>();
//                    detailLink.put("FEntity_Link_FFlowId", "");//业务流程图
//                    detailLink.put("FEntity_Link_FFlowLineId", "");//推进路线
//                    detailLink.put("FEntity_Link_FRuleId", "PRD_MO2INSTOCK");//转换规则
//                    detailLink.put("FEntity_Link_FSTableId", srcPoorInfo.get("srcId"));//源单表内码
//                    detailLink.put("FEntity_Link_FSTableName", "T_PRD_MOENTRY");//源单表
//                    detailLink.put("FEntity_Link_FSBillId", srcPoorInfo.get("srcId"));//源单内码
//                    detailLink.put("FEntity_Link_FSId", Integer.parseInt(srcEntryId));//源单分录内码
//                    detailLinkList.add(detailLink);
//                    detailEntity.put("FEntity_Link", detailLinkList);//源单关联关系 必填
                }
                detailEntity.put("FMustQty", srcBillEntry.getQty());//应发数
                detailEntity.put("FRealQty", srcBillEntry.getQty());//实发数
                detailEntity.put("F_WMVJ_Text_qtr1", srcBillEntry.getReserved1());//实发数
//                //辅助属性
//                Map<String,Object> auxPropId = new LinkedHashMap<>();
//                Map<String,Object> level = new LinkedHashMap<>();
//                level.put("fnumber", srcBillEntry.getReserved1());//等级
//                Map<String,Object> ply = new LinkedHashMap<>();
//                ply.put("fnumber", srcBillEntry.getReserved2());//厚度
//                auxPropId.put("FAUXPROPID__FF100001", level);
//                auxPropId.put("FAUXPROPID__FF100002", ply);
//                //辅助属性
//                detailEntity.put("FAuxPropId", auxPropId);
                if(!StringUtils.isEmpty(srcBillEntry.getStockNumber())){
                    HashMap<String, Object> stockId = new HashMap<>();
                    stockId.put("fnumber", srcBillEntry.getStockNumber());
                    detailEntity.put("FStockId", stockId);//仓库 必填
                }
                if (!StringUtils.isEmpty(srcBillEntry.getStockLocNumber())){
                    String stockLocFlexNumber = sqlTool.getStockLocFlexNumber(srcBillEntry.getStockLocNumber(), srcBillEntry.getStockNumber());
                    //仓位
                    Map<String, Object> shippingSpaceNumber = new HashMap<>(); //位置仓位值编码 必填
                    shippingSpaceNumber.put("FNumber", srcBillEntry.getStockLocNumber());
                    Map<String, Object> stockLocId = new HashMap<>();
                    stockLocId.put("FSTOCKLOCID__" + stockLocFlexNumber, shippingSpaceNumber);
                    detailEntity.put("FStockLocId", stockLocId);
                }
                detailEntityList.add(detailEntity);
            }
        }
        //修改
        DataObject order = new DataObject(FormIdEnum.salOutStock);
        order.setAttribute("FID", fid);
        order.setAttribute("FEntity", detailEntityList);//订单明细
        try {
            Map<String, Object> result = sqlTool.editForDataObject(order);
            if (result.get("code").toString().equals("200")){
                return RequestResult.ofSuccess(result);
            }else {
                return RequestResult.ofError(result.get("msg").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProcedureException("无法调用云星空接口");
        }
    }

    @Override
    public void postPosition(String billNo) {

    }

    @Override
    public void auditTargetBill(String billNo) {
        //提交审核
        sqlTool.auditBill(billNo, FormIdEnum.salOutStock);
    }

    @Override
    public void deleteTargetBill(String fid) {
        sqlTool.deleteBill(fid, FormIdEnum.salOutStock);
    }
}
